package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.lendItemReturn.LendItemReturnAddForm;
import com.yikes.srb.leadingModle.form.lendItemReturn.LendItemReturnEditForm;
import com.yikes.srb.leadingModle.form.lendItemReturn.LendItemReturnQuery;
import com.yikes.srb.leadingModle.vo.LendItemReturnVO;
import com.yikes.srb.service.LendItemReturnService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标的出借回款记录表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "LendItemReturnController", description = "标的出借回款记录表")
@RestController
@RequestMapping("/admin/lend_item_return")
public class LendItemReturnController {

    private final LendItemReturnService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<LendItemReturnVO>> listPage(LendItemReturnQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<LendItemReturnVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody LendItemReturnAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody LendItemReturnEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "获取列表")
    @GetMapping("/auth/list/{lendId}")
    public Result list(
            @Parameter(name = "标的id", required = true)
            @PathVariable Long lendId, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        List<LendItemReturnVO> list = service.selectByLendId(lendId, userId);
        return Result.ok(list);
    }

}
